package nl.ceasaro.jeslee.core.dao;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.springframework.test.annotation.AbstractAnnotationAwareTransactionalTests;

/**
 * Created by IntelliJ IDEA.
 * User: cees
 * Date: Dec 4, 2007
 * Time: 12:01:27 AM
 * <p>
 * Abstract testclass for testing hibernated driven dao's.
 * </p>
 */
//@ContextConfiguration(locations = {"spring/core-beans.xml", "spring/hibernate-config-test-beans.xml"})
//@RunWith(JUnit4ClassRunner.class)
public abstract class AbstractHibernateTestCase extends AbstractAnnotationAwareTransactionalTests
{
   private SessionFactory sessionFactory;


   @Override
   protected String[] getConfigLocations()
   {
      return new String[]{"spring/jeslee-core-beans.xml", "spring/hibernate-config-test-beans.xml"};
   }


   /**
    * Utility method to delete some test instances from the hibernate session cache
    * Do not (re)use the intances passed into this method, after this call they are
    * non-persistent/transient instances
    *
    * @param persistantInstances persistant instance to remove.
    */
   protected void removeFromHibernateSession(Object... persistantInstances)
   {
      //First flush everything to DB
      getSession().flush();
      //Then clear hibernate session
      for (Object o : persistantInstances)
      {
         getSession().evict(o);
      }
   }

   public Session getSession()
   {
      return getSessionFactory().getCurrentSession();
   }

   public SessionFactory getSessionFactory()
   {
      return sessionFactory;
   }

   public void setSessionFactory(SessionFactory sessionFactory)
   {
      this.sessionFactory = sessionFactory;
   }

}
